using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PVID_UHF_CE_Application
{
    public partial class GraphForm : Form
    {
        public GraphForm()
        {
            InitializeComponent();
        }
        private bool drawBack = true;
        private Bitmap _backBuffer;
        protected override void OnPaintBackground(PaintEventArgs e)
        {

            if (drawBack)
            {
                base.OnPaintBackground(e);
                drawBack = false;
             }
        }
        private void GraphForm_Load(object sender, EventArgs e)
        {
            //MessageBox.Show(V.Length.ToString());
            //MessageBox.Show(V[2].ToString());
            button1_Click(sender, e);
            Invalidate();
        }
        public double[] V;// = new double[] { 0, 6, 12.2, 16.4, 22.2, 25.1, 26.2, 26.6, 27.4};
        public double[] I;// = new double[] { 7.3, 7.2, 7.1, 7.0, 6.8, 5.8, 4.2, 3.1, 0.0 };
        private void DrawGraph()
        {
            try
            {
                Brush b = new SolidBrush(Color.Red);
                _backBuffer = new Bitmap(250, 165);
                Graphics g = Graphics.FromImage(_backBuffer);
                g.DrawImage(resImages.GraphBackRefex1, 0, 0);
                Graphics t = this.CreateGraphics();
                Pen linePen = new Pen(Color.Black);//.FromArgb(240,237,143));
                //g.DrawLine(linePen, 0, 0, 50, 50);
                //g.DrawEllipse(linePen, 0, 0, 5, 5);
                //g.FillEllipse(b, 51, 51, 5, 5);
                int x1, y1, x2, y2;
                x1 = y1 = x2 = y2 = 0;
                for (int i = 0; i <= 8; i++)
                {
                    //MessageBox.Show(String.Format("{0},{1}", I[i].ToString(), V[i].ToString()));
                    int _V = Convert.ToInt32(Math.Round((V[i]) * 7.4, 0).ToString("0"));
                    int _I = 150 - Convert.ToInt32(Math.Round((I[i] * 19), 0).ToString());
                    if (_I == 150)
                        _I = 145;
                    g.FillEllipse(b, _V-2, _I-2, 5, 5);
                    if (x1 != 0 || y1 != 0)
                    {
                        x2 = x1; y2 = y1;
                        x1 = _V; y1 = _I;
                        g.DrawLine(linePen, x1, y1, x2, y2);
                    }
                    else
                    {
                        x1 = _V; y1 = _I;
                    }

                    //MessageBox.Show(String.Format("{0},{1}", _I.ToString(), _V.ToString()));
                }
                t.DrawImage(_backBuffer, 2, 45);
                Invalidate();
                t.Dispose();
                g.Dispose();
                _backBuffer.Dispose();
                linePen.Dispose();
                Invalidate();
                //MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DrawGraph();
        }
    }
} 